package org.bot.api.config;

import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ClientConfig {

    static Logger logger = LoggerFactory.getLogger(ClientConfig.class);

    Properties properties;

    final static String configPath = System.getProperty("user.dir") + "/config/bot-config.properties";

    @Getter
    static ClientConfig botConfig;

    static {
        init();
    }

    public static void buildClientConfig(Properties properties) {
        botConfig = new ClientConfig(properties);
    }

    private static void init() {
        FileInputStream in = null;
        try {
            in = new FileInputStream(configPath);
        } catch (FileNotFoundException e) {
            logger.error("读取配置异常:", e);
        }
        if (in == null) {
            throw new RuntimeException("配置文件读取错误");
        }
        Properties properties = new Properties();
        try {
            properties.load(in);
            ClientConfig.buildClientConfig(properties);
        } catch (IOException e) {
            logger.error("读取配置异常:", e);
        }
    }

    private ClientConfig(Properties properties) {
        this.properties = properties;
    }

    public String getDrive() {
        return this.properties.getProperty("bot.drive");
    }

    public String getBinary() {
        return this.properties.getProperty("bot.binary");
    }

    public String getDriveName() {
        return this.properties.getProperty("bot.drive.name");
    }


    public Long getPageLoadTimeout() {
        String timeout = this.properties.getProperty("page.load.timeout");
        if (timeout == null) {
            return null;
        }
        return Long.parseLong(timeout);
    }

    public int getMaxTask() {
        String property = this.properties.getProperty("bot.max.task");
        if (property == null) {
            return 1;
        }
        return Integer.parseInt(property);
    }

    public int getMaxQueue() {
        String property = this.properties.getProperty("bot.max.queue");
        if (property == null) {
            return 100;
        }
        return Integer.parseInt(property);
    }


    public String[] getArguments() {
        String arg = this.properties.getProperty("bot.arguments");
        if (arg == null) {
            return new String[]{};
        }
        return arg.split(" ");
    }

}
